package com.front.web.lvyou_specialty_front;

import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.lvyou.domain.LvyouMember;
import com.bootdo.lvyou.service.LvyouMemberService;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyUserAddress;
import com.bootdo.lvyou_specialty.service.LvyouSpecialtyUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/f/userAddressController")
public class UserAddressFrontController {

    /*
    方法列表
     *1、
     * 得到用户的收货地址列表
     * http://localhost/f/userAddressControllergetMemberHarvestAddress
     *
     * 2、
     * 用户添加收货地址
     * http://localhost/f/userAddressController/addHarvestAddress
     *
     * 3、
     * 删除收货地址
     * http://localhost/f/userAddressController/deleteHarvestAddress
     *
     * 4、
     * 得到某一个收货地址的信息
     * http://localhost/f/userAddressController/getOneHarvestAddress
     *
     * 5、
     * 设为默认地址
     * http://localhost/f/userAddressController/setDefaultAddress
     *
     */

    @Autowired
    private LvyouSpecialtyUserAddressService lvyouSpecialtyUserAddressService;

    @Autowired
    private LvyouMemberService lvyouMemberService;

    /**
     * 得到用户的收货地址列表
     * http://localhost/f/userAddressController/getMemberHarvestAddress
     * @param uid
     * @param request
     * @return
     */
    @RequestMapping("getMemberHarvestAddress")
    public R getMemberHarvestAddress(@RequestParam(value = "uid", defaultValue = "0")Long uid, HttpServletRequest request) {

        LvyouMember lvyouMember = lvyouMemberService.get(uid);
        if (lvyouMember == null) {
            return R.error("用户参数错误");
        }

        Map<String, Object> param = new HashMap<>();
        param.put("uid", uid);
        List<LvyouSpecialtyUserAddress> list = lvyouSpecialtyUserAddressService.list(param);
        boolean flag = false;
//        for(int i = 0; i < list.size(); i++) {
//            Integer isdefault = list.get(i).getIsDefault();
//            if (isdefault == 1) {
//                LvyouSpecialtyUserAddress address = list.get(i);
//                list.remove(i);
//                list.add(0, address);
//                flag = true;
//                break;
//            }
//        }
        return R.ok().put("l",list).put("flag", flag);
    }

    /**
     * 用户添加收货地址
     * http://localhost/f/userAddressController/addHarvestAddress
     * @param uid 用户的id
     * @param lvyouSpecialtyUserAddress 要添加的收货地址对象
     *        参数：receiverName  收货人姓名
     *               receiverMobile  收货人联系方式
     *                receiverAddress  收货人地址
     *
     *
     * @param request
     * @return
     */
    @RequestMapping("addHarvestAddress")
    public R addHarvestAddress(@RequestParam(value = "uid", defaultValue = "0")Long uid,
                               LvyouSpecialtyUserAddress lvyouSpecialtyUserAddress,
                               HttpServletRequest request) {

        if (uid == 0) {
            return R.error("参数错误");
        }

        // 得到用户信息
        LvyouMember lvyouMember = lvyouMemberService.get(uid);
        if (lvyouMember == null) {
            return R.error("用户信息错误");
        }

        if (StringUtils.isEmpty(lvyouSpecialtyUserAddress.getReceiverAddress()) || StringUtils.isEmpty(lvyouSpecialtyUserAddress.getReceiverMobile())|| StringUtils.isEmpty(lvyouSpecialtyUserAddress.getReceiverName())) {
            return R.error("参数为空");
        }

        lvyouSpecialtyUserAddress.setUid(lvyouMember.getId());
        lvyouSpecialtyUserAddress.setUserName(lvyouMember.getNickname());

        // 查看数据库中用户的收货地址,flag为true代表没有默认的收货地址
        // 如果有就直接保存，如果没有就设置为默认的并保存
        boolean addressFlag = true;
        List<LvyouSpecialtyUserAddress> byMid = lvyouSpecialtyUserAddressService.getByUid(uid);
        for (LvyouSpecialtyUserAddress one : byMid) {
            if (null != one) {
                addressFlag = false;
                break;
            }
        }
        if (addressFlag) {
            lvyouSpecialtyUserAddress.setIsDefault(1);
        } else {
            LvyouSpecialtyUserAddress oldDefault = byMid.get(0);
            if (lvyouSpecialtyUserAddress.getIsDefault() == null) {
                lvyouSpecialtyUserAddress.setIsDefault(0);
            }

            if (lvyouSpecialtyUserAddress.getIsDefault() == 1) {
                if (lvyouSpecialtyUserAddress.getId() == null) {
                    oldDefault.setIsDefault(0);
                    lvyouSpecialtyUserAddressService.update(oldDefault);
                }

            } else {
                if (lvyouSpecialtyUserAddress.getId() != null && oldDefault.getId().longValue() == lvyouSpecialtyUserAddress.getId().longValue()) {
                    lvyouSpecialtyUserAddress.setIsDefault(1);
                }
            }
        }
        // 进行保存
        int save = 0;
//        if (lvyouSpecialtyUserAddress.getId() != null) {
//            save = lvyouSpecialtyUserAddressService.update(lvyouSpecialtyUserAddress);
//        } else {
//            save = lvyouSpecialtyUserAddressService.save(lvyouSpecialtyUserAddress);
//        }
//
        save = lvyouSpecialtyUserAddressService.save(lvyouSpecialtyUserAddress);
        if (save > 0) {
            return R.ok("操作成功");
        }
        return R.error("保存失败");
    }

    /**
     * 删除收货地址
     * http://localhost/f/userAddressController/deleteHarvestAddress
     * @param uid
     * @param
     * @param request
     * @return
     */
    @RequestMapping("deleteHarvestAddress")
    public R deleteHarvestAddress(@RequestParam(value = "uid", defaultValue = "0")Long uid,
                                  @RequestParam(value = "harvestId",defaultValue = "0")Long harvestId,
                                  HttpServletRequest request) {

        if (uid == 0 || harvestId == 0) {
            return R.error("参数错误");
        }

        // 得到用户信息
        LvyouMember lvyouMember = lvyouMemberService.get(uid);

        // 得到地址信息
        LvyouSpecialtyUserAddress address = lvyouSpecialtyUserAddressService.get(harvestId);
        if (uid.longValue() != address.getUid().longValue()) {
            return R.error("您不能删除他人的收货地址");
        }


        // 进行删除操作
        int save = lvyouSpecialtyUserAddressService.remove(harvestId);
        if (save > 0) {
            return R.ok("删除成功");
        }
        return R.error("删除失败");
    }

    /**
     * 得到某一个收货地址的信息
     * http://localhost/f/userAddressController/getOneHarvestAddress
     * @param uid
     * @param harvestId
     * @param request
     * @return
     */
    @RequestMapping("getOneHarvestAddress")
    public R getOneHarvestAddress(@RequestParam(value = "uid", defaultValue = "0")Long uid,
                                  @RequestParam(value = "harvestId",defaultValue = "0")Long harvestId,
                                  HttpServletRequest request) {

        if (uid == 0 || harvestId == 0) {
            return R.error("参数错误");
        }

        // 得到地址信息
        LvyouSpecialtyUserAddress address = lvyouSpecialtyUserAddressService.get(harvestId);
        if (address == null) {
            return R.error("您的收货地址信息有误");
        }
        if (address.getUid().longValue() != uid) {
            return R.error("您不能编辑他人的收货地址");
        }

        // 进行删除操作

        return R.ok().put("i", address);
    }

    /**
     * 设为默认地址
     * http://localhost/f/userAddressController/setDefaultAddress
     * @param uid
     * @param harvestId
     * @param request
     * @return
     */
    @RequestMapping("setDefaultAddress")
    public R setDefaultAddress(@RequestParam(value = "uid", defaultValue = "0")Long uid,
                               @RequestParam(value = "harvestId",defaultValue = "0")Long harvestId,
                               HttpServletRequest request) {

        if (uid == 0 || harvestId == 0) {
            return R.error("参数错误");
        }

        // 得到地址信息
        LvyouSpecialtyUserAddress address = lvyouSpecialtyUserAddressService.get(harvestId);
        if (address == null) {
            return R.error("收货地址信息不正确");
        }
        if (address.getUid().longValue() != uid) {
            return R.error("您不能编辑他人的收货地址");
        }

        // 查询用户的默认收货地址
        List<LvyouSpecialtyUserAddress> byMid = lvyouSpecialtyUserAddressService.getByUid(uid);

        LvyouSpecialtyUserAddress oldaddress = byMid.get(0);

        if (oldaddress.getId().longValue() == address.getId()) {
            return R.error("该地址已经是默认地址了");
        }

        oldaddress.setIsDefault(0);
        address.setIsDefault(1);

        if (lvyouSpecialtyUserAddressService.setDefault(oldaddress, address) == 0) {
            return R.error();
        }
        return R.ok().put("i", address);
    }

    /**
     * http://localhost/f/userAddressController/getDefaultAddress
     * @param uid
     * @param request
     * @return
     */
    @RequestMapping("getDefaultAddress")
    public R setDefaultAddress(@RequestParam(value = "uid", defaultValue = "0")Long uid,
                               HttpServletRequest request) {

        if (uid == 0) {
            return R.error("参数错误");
        }

        LvyouMember lvyouMember = lvyouMemberService.get(uid);
        if (lvyouMember == null) {
            return R.error("用户信息错误");
        }

        // 得到地址信息
        List<LvyouSpecialtyUserAddress> defaultAddress = lvyouSpecialtyUserAddressService.getByUid(uid);

        if (defaultAddress.size() == 0) {
            return R.error();
        }

        LvyouSpecialtyUserAddress lvyouSpecialtyUserAddress = defaultAddress.get(0);

        if (lvyouSpecialtyUserAddress == null) {
            return R.error();
        }
        if (lvyouSpecialtyUserAddress.getUid().longValue() != uid) {
            return R.error();
        }


        LvyouSpecialtyUserAddress oldaddress = defaultAddress.get(0);

        return R.ok().put("i", oldaddress);
    }

}
